package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.mapper.AuthorizationMapper;
import com.WISSC.service.AuthorizationImageService;
import com.WISSC.service.AuthorizationService;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 授权机构信息表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Service
public class AuthorizationServiceImpl extends ServiceImpl<AuthorizationMapper, Authorization> implements AuthorizationService {

    @Autowired
    AuthorizationImageService authorizationImageService;


    /**
     * @param current
     * @param size
     * @param unitName
     * @param startTime
     * @param city
     * @param grantType
     * @param grantLevel
     * @return 查询 + 分页
     */
    @Override
    public ResultPage<Authorization> selectMyPage(int current, int size, String unitName, Long startTime, String city, Integer grantType, Integer grantLevel,HttpSession session) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (current - 1) * size);
        paramMap.put("limit", size);
        paramMap.put("unitName", unitName);
        paramMap.put("startTime", startTime);
        paramMap.put("city", city);
        paramMap.put("grantType", grantType);
        paramMap.put("grantLevel", grantLevel);
        User user = (User) session.getAttribute("user");
        if (user.getUsername().equals("admin")){
            paramMap.put("type", "");
        }else{
            paramMap.put("type", 1);
        }        List<Authorization> list = baseMapper.selectAuthorization(paramMap);
        Integer count = baseMapper.countAuthorization(paramMap);
        ResultPage<Authorization> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * @param authorization
     * @param pIds
     * @return 机构信息添加
     * @throws SQLException
     */
    @Override
    public String authorizationInsert(Authorization authorization, String[] pIds){
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        //创建日期
        authorization.setCreateTime(DateUtil.date());
        this.save(authorization);
        for (int i = 0; i < pIds.length; i++) {
            AuthorizationImage authorizationImage = new AuthorizationImage();
            authorizationImage.setId(Long.parseLong(pIds[i]));
            authorizationImage.setGrantId(authorization.getId());
            if (!authorizationImageService.updateById(authorizationImage)) {
                jsonMap.put("flag", false);
                jsonMap.put("msg", "保存失败");
                return JSON.toJSONString(jsonMap);
            }
        }
        jsonMap.put("flag", true);
        jsonMap.put("msg", "保存成功");
        return JSON.toJSONString(jsonMap);
    }

    /**
     * @param authorization
     * @param session1
     * @return 机构登陆
     */
    @Override
    public boolean login(Authorization authorization, HttpSession session1) {
        Map  map = new HashMap<>();
        map.put("unitName",authorization.getUnitName());
        List<Authorization> users = baseMapper.login(map);
        //如果用户存在，放入session域
        if(users.size()>0) {
            session1.setAttribute("unitName", users.get(0));
            return  true;
        }else {
            return false;
        }
    }
    /**
     *
     * @param grantId
     * @return 机构图片回显--url
     */
    @Override
    public Map getImageUrl(Long grantId) {
        Map map = new HashMap();
        String str = "";
        List<AuthorizationImage> list = authorizationImageService.getImageUrl(grantId);
        for (int i = 0; i <list.size() ; i++) {
            String url = list.get(i).getUrl();
            str +=","+url;
        }
        String[] arr = str.split(",");
        map.put("data",arr);
        return map;
    }

    /**
     * @return 授权机构查询
     */
    @Override
    public List<Authorization> selectAuthorizationByUnitname(Authorization authorization) {
        List list = new ArrayList();
        if(authorization.getUnitName()!=null && authorization.getUnitName()!=""){
            list = baseMapper.selectAuthorizationByUnitName(authorization.getUnitName());
        }
        return list;
    }


    /**
     * @param id
     * @return 战队编辑回显city
     */
    @Override
    public  String getTeamCity(Long id) {
        Authorization  t= baseMapper.getTeamCity(Long.parseLong(id+""));
        String str = t.getCity();
        System.out.println("str:"+str);
        return str;
    }

    @Override
    public Authorization countPerson() {
        return baseMapper.countPerson();
    }


    @Override
    public Map updateAuthorization(Authorization authorization, String[] pIds) {
        HashMap<Object, Object> map = new HashMap<>();
        if (this.updateById(authorization)) {
            if (pIds[0].equals("kk")) {
                return ResponseUtil.getSuccessMap();
            }else{
                authorizationImageService.deleteByIdGrand(authorization.getId());
                for (int i = 0; i < pIds.length; i++) {
                    AuthorizationImage authorizationImage = new AuthorizationImage();
                    authorizationImage.setId(Long.parseLong(pIds[i]));
                    authorizationImage.setGrantId(authorization.getId());
                    if (!authorizationImageService.updateById(authorizationImage)) {
                        return  ResponseUtil.getExceptionMap();
                    }
                }
            }
            return ResponseUtil.getSuccessMap();
        }
        return  ResponseUtil.getExceptionMap();
    }
}
